import config from "./config"
import com from "./com"

export default {
    getBaseUrl() {
        return config.baseUrl[config.baseUrlIndex]
    },
    getAssetBaseUrl() {
        if (config.assetType === 2) {
            return config.assetUrl
        }
        return config.baseUrl[config.baseUrlIndex];
    },
    getCookie(headers) {
        var cookies = [];
        for (var i in headers) {
            if (i == "Set-Cookie") {
                var arr = headers[i].split(";")
                cookies = arr[0];
            }
        }
        return cookies;
    },
    getAccessTokenFromHeader(headers) {
        let access_token = headers["access-token"]
        if (!com.isbool(access_token)) {
            return ""
        }
        com.setLocData("access_token", access_token)
        return access_token;
    },
    getAuthToken() {
        var accessToken = com.getLocData("auth_token")
        return accessToken;
    },
    setAuthToken(auth_token) {
        com.setLocData("auth_token",auth_token)
    },
    createHeaders() {
        let header = {
            "Content-Type": "application/json"
        }
        let access_token = com.getLocData("access_token")
        if (com.isbool(access_token)) {
            header["access-token"] = access_token
        }
        let auth_token = com.getLocData("auth_token")
        if (com.isbool(auth_token)) {
            header["X-AUTH-TOKEN"] = auth_token
        }
    }
    ,
    get(url, data, cbFunc) {
        let headers = this.createHeaders()
        let _this = this
        uni.request({
            url: this.getBaseUrl() + url, //仅为示例，并非真实接口地址。
            method: "GET",
            data: data,
            dataType: "json",
            header: headers,
            success: (res) => {
                _this.getAccessTokenFromHeader(res.header)
                cbFunc(res.data,res.statusCode)
            },
            fail: (res) => {
        		com.showToast("网络错误")
            },
        	complete: (res)=>{
        	}
        });
    },
    post(url, data, cbFunc) {
        let headers = this.createHeaders()
        let _this = this
        uni.request({
            url: this.getBaseUrl() + url, //仅为示例，并非真实接口地址。
            method: "POST",
            data: JSON.stringify(data),
            dataType: 'json',
            header: headers,
            success: (res) => {
                _this.getAccessTokenFromHeader(res.header)
                cbFunc(res.data, res.statusCode)
            },
            fail: (res) => {
                if (com.isbool(res.message)) {
                    com.showToast(res.message);
                } else {
                    com.showToast("网络错误");
                }
            },
            complete: (res) => {

            }
        });
    },
    upload(url, filePath, cbFunc) {
        let headers = this.createHeaders()
        uni.uploadFile({
            url: this.getBaseUrl() + url, //仅为示例，非真实的接口地址
            filePath: filePath,
            name: 'Filedata',
            dataType: "json",
            header: headers,
            formData: {},
            success: (uploadFileRes) => {
                let data = JSON.parse(uploadFileRes.data)
                cbFunc(data)
            }
        });
    }
}